package com.bfly.mall.message.api;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.mall.message.entity.Notify;
import com.bfly.mall.message.dto.NotifyListDTO;
import com.bfly.mall.message.service.INotifyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统通知APIController
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/11/1 10:47
 */
@RestController
@RequestMapping(value = "/api/notify")
public class NotifyApiController extends BaseController {

    @Autowired
    private INotifyService notifyService;

    /**
     * 系统通知列表
     * @author 长沙商脉科技有限公司
     * @date 2020/11/1 10:49
     */
    @GetMapping(value = "/list")
    public void listNotify(HttpServletResponse response, HttpServletRequest request) {
        Map<String, Object> exact = new HashMap<>(1);
        // 查询为撤销的通知
        exact.put("is_revoke", false);
        List<Notify> notifies = notifyService.getList(exact);
        List<NotifyListDTO> list = new ArrayList<>();
        if (notifies != null) {
            for (Notify notify : notifies) {
                NotifyListDTO dto = new NotifyListDTO();
                BeanUtils.copyProperties(notify, dto);
                list.add(dto);
            }
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(list));
    }

    /**
     * 系统通知详情
     * @author 长沙商脉科技有限公司
     * @date 2020/11/2 12:13
     */
    @GetMapping(value = "/detail")
    public void detailNotify(@RequestParam("notifyId")int notifyId, HttpServletResponse response){
        Notify notify=notifyService.get(notifyId);
        notify.setViews(notify.getViews()+1);
        notifyService.edit(notify);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(notify));
    }
}